package blog.utils;

import java.text.MessageFormat;
import java.util.List;
import java.util.Map;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;

public class EmailFactory {
    private static final String AT_SYMBOL = "@";
    private static final String NULL_ANCHOR = "#";

    private DefaultAuthenticator authenticator;
    private String hostName;
    private String charset;
    private String subject;
    private String template;
    private String from;
    private List<String> to;
    private List<String> postOffices;

    private Map<String, String> templates;
    private Map<String, String> subjects;

    private String websiteUrl;

    // private String initCheckTemplate;
    // private String viewSignupInfo;

    public HtmlEmail createHtmlEmail() throws EmailException {
	HtmlEmail email = new HtmlEmail();
	setEmail(email);
	return email;
    }

    public HtmlEmail createHtmlEmail(String subject, String templateType)
	    throws EmailException {
	HtmlEmail email = new HtmlEmail();
	this.subject = subject;
	this.template = templateType;
	// if(templateType.equals("viewSignupInfo")){
	// this.template = viewSignupInfo;
	// } else if(templateType.equals("initCheckTemplate")){
	// this.template = initCheckTemplate;
	// }
	setEmail(email);
	return email;
    }

    public HtmlEmail createHtmlEmailByType(String mailType)
	    throws EmailException {
	HtmlEmail email = new HtmlEmail();
	this.subject = subjects.get(mailType);
	this.template = templates.get(mailType);
	setEmail(email);
	return email;
    }

    /**
     * 填充模板生成内容
     * 
     * @param arguments
     * @return
     */
    public String fromTemplate(Object... arguments) {
	return MessageFormat.format(template, arguments);
    }

    /**
     * 取得邮件接收者的邮箱登陆口
     * 
     * @return
     */
    public String getPostOffice(String mail) {
	if (CollectionUtils.isEmpty(postOffices) || StringUtils.isEmpty(mail))
	    return NULL_ANCHOR;

	final String PROTOCAL_PREFIX = "http://";
	final String PRESUME_DOMAIN = "mail.";

	String suffix = mail.substring(mail.indexOf(AT_SYMBOL) + 1)
		.toLowerCase();
	for (String postOffice : postOffices) {
	    if (postOffice.endsWith(suffix))
		return PROTOCAL_PREFIX + postOffice;
	}
	return PROTOCAL_PREFIX + PRESUME_DOMAIN + suffix;
    }

    private void setEmail(Email email) throws EmailException {
	email.setAuthenticator(authenticator);
	email.setHostName(hostName);
	email.setCharset(charset);
	email.setSubject(subject);
	email.setFrom(from);

	if (!(null == to || to.isEmpty()))
	    email.setTo(to);
    }

    public void setAuthenticator(DefaultAuthenticator authenticator) {
	this.authenticator = authenticator;
    }

    public void setHostName(String hostName) {
	this.hostName = hostName;
    }

    public void setCharset(String charset) {
	this.charset = charset;
    }

    public void setSubject(String subject) {
	this.subject = subject;
    }

    public void setFrom(String from) {
	this.from = from;
    }

    public void setPostOffices(List<String> postOffices) {
	this.postOffices = postOffices;
    }

    public void setTemplate(String template) {
	this.template = template;
    }

    public void setTemplates(Map<String, String> templates) {
	this.templates = templates;
    }

    public void setSubjects(Map<String, String> subjects) {
	this.subjects = subjects;
    }

    public String getWebsiteUrl() {
	return websiteUrl;
    }

    public void setWebsiteUrl(String siteUrl) {
	if (!siteUrl.endsWith("/")) {
	    this.websiteUrl = siteUrl + "/";
	} else {
	    this.websiteUrl = siteUrl;
	}
    }

}
